package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.example.zzl.adapter.HomeFragment.HotSpecialAdapter;
import com.example.zzl.databinding.ActivityHotSpecialBinding;
import com.example.zzl.model.HomeFragment.HotSpecialModel;
import com.example.zzl.model.bean.Home;
import com.example.zzl.view.activity.SpecialInfoActivity;
import com.example.zzl.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/11.
 */

public class HotSpecialPresenter implements BaseViewModel, AdapterView.OnItemClickListener {

    private  HotSpecialModel model;
    private Context context;
    private HotSpecialCallback callback;
    private ActivityHotSpecialBinding db;
    private List<Home.Special.ResultBean> list;
    private HotSpecialAdapter adapter;

    public HotSpecialPresenter(Context context, HotSpecialCallback callback, ActivityHotSpecialBinding db) {
        this.context = context;
        this.callback = callback;
        this.db = db;
        model = new HotSpecialModel(context, this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("headTitle", list.get(position).strSpecialName);
        bundle.putInt("iSpecialID", list.get(position).iSpecialID);
        callback.startActivity(SpecialInfoActivity.class, bundle);
    }


    public interface HotSpecialCallback extends View.OnClickListener{
        String getHeadTitle();
        void startActivity(Class cls, Bundle bundle);
    }

    @Override
    public void init() {
        list = new ArrayList<Home.Special.ResultBean>();
        adapter = new HotSpecialAdapter(context, list, 0, this);
    }

    @Override
    public void initView() {
        db.include.toolbar.setNavigationOnClickListener(callback);
        db.setHeadTitle(callback.getHeadTitle());
        db.lv.setOnItemClickListener(this);
        db.lv.setAdapter(adapter);
    }

    @Override
    public void loadFinished() {
        list.clear();
        list.addAll(model.getList());
        adapter.notifyDataSetChanged();
    }


    @Override
    public void loadData(String parameter) {
        model.loadData(parameter);
    }
}
